﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using WF_Business;

namespace ExtBusiness.Common
{
    public class Notice
    {
        public static DataTable GetNotice()
        {
            string strSql = "select * from st_other t where t.is_notice='1'";
            DataTable dt;
            SysParams.OAConnection().RunSql(strSql, out dt);
            return dt;
        }
        /// <summary>
        /// 查看是否存在信息
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public string GetNotifyInfo(string iid)
        {
            string sHaveInfo = "0";
            string strSql = string.Format("select * from ST_MEETINGNOTIFY t where t.iid='{0}'", iid);
            DataTable dt;
            SysParams.OAConnection().RunSql(strSql, out dt);
            if (dt.Rows.Count > 0)
            {
                sHaveInfo = "1";
            }
            return sHaveInfo;
        }
        /// <summary>
        /// 向通知人员列表中插入数据
        /// </summary>
        /// <param name="userlist"></param>
        /// <param name="IID"></param>
        public void InsertMeetingNotifyUser(string userlist, string IID)
        {
            string sql = string.Format("delete from ST_MEETINGNOTIFYUSER t where t.iid='{0}'", IID);
            SysParams.OAConnection().RunSql(sql);
            string[] arrSend = userlist.Substring(0, userlist.Length - 1).Split('|');
            for (int i = 0; i < arrSend.Length; i++)
            {
                sql = string.Format(@"insert into ST_MEETINGNOTIFYUSER (IID, LOGIN_NAME) values('{0}','{1}')", IID, arrSend[i]);
                SysParams.OAConnection().RunSql(sql);
            }
        }

        /// <summary>
        /// 获取登录人员是否具有通知
        /// </summary>
        /// <param name="RealUserId"></param>
        /// <returns></returns>
        public DataTable GetMettingNotiyUser(string RealUserId)
        {
            string strSql = string.Format("select * from ST_MEETINGNOTIFYUSER t where t.login_name='{0}' and t.readflag='0' and type='0'", RealUserId);
            DataTable dt = null;
            SysParams.OAConnection().RunSql(strSql, out dt);
            return dt;
        }

        /// <summary>
        /// 获取会议通知未读消息
        /// </summary>
        /// <param name="RealUserId"></param>
        /// <returns></returns>
        public DataTable GetMettingNotiyInfo(string RealUserId)
        {
            string strSql = string.Format("select a.*,t.id tid from ST_MEETINGNOTIFYUSER t ,st_meetingnotify a where t.login_name='{0}' and t.readflag='0' and a.iid = t.iid ", RealUserId);
            DataTable dt = null;
            SysParams.OAConnection().RunSql(strSql, out dt);
            return dt;
        }

        /// <summary>
        /// 更新阅读标志位
        /// </summary>
        /// <param name="id"></param>
        public void UpdateNotifyUserFlag(string id)
        {
            string strSql = string.Format("update ST_MEETINGNOTIFYUSER t set t.readflag=1 where t.id='{0}'", id);
            SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 获取所有会议通知消息
        /// </summary>
        /// <param name="whereValue"></param>
        /// <returns></returns>
        public DataTable GetMeetingNotiyInfoAll(string whereValue)
        {
            string strSql = string.Format(@"select a.*,t.id tid,b.name,t.readflag from ST_MEETINGNOTIFYUSER t,st_meetingnotify a,st_instance b 
                                            where 1=1 and a.iid = t.iid and a.iid = b.iid and t.type='0' {0} order by t.insert_time desc", whereValue);
            DataTable dt;
            SysParams.OAConnection().RunSql(strSql, out dt);
            return dt;
        }

        #region 删除会议通知记录
        /// <summary>
        /// 删除会议通知记录
        /// </summary>
        /// <param name="id"></param>
        public void DelMeetingNoticyBrowse(string id)
        {
            string strSql = string.Format(@"delete from ST_MEETINGNOTIFYUSER t where t.ID = '{0}'", id);
            SysParams.OAConnection().RunSql(strSql);
        }
        #endregion

        #region 设定弹出窗口接收人员(所有)
        /// <summary>
        /// 设定弹出窗口接收人员
        /// </summary>
        /// <param name="sID"></param>
        public void InsertNoticeUser(string sID)
        {
            string strSql = string.Format(@"insert into ST_NOTIFYUSER t (LOGIN_NAME,SID) select login_name,'{0}' from st_user t", sID);
            SysParams.OAConnection().RunSql(strSql);
        }
        #endregion

        #region 弹出窗口接收人员
        /// <summary>
        /// 弹出窗口接收人员
        /// </summary>
        /// <param name="sID"></param>
        public void InsertNoticeUser(string sID,int readflag)
        {
            string strSql = string.Format(@"select t.login_name from st_user t ");
            DataTable dt;
            SysParams.OAConnection().RunSql(strSql, out dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strSql = string.Format(@"insert into ST_NOTIFYUSER t (LOGIN_NAME,SID,readflag) values ('{0}','{1}','{2}')", dt.Rows[i][0].ToString(), sID,readflag);
                SysParams.OAConnection().RunSql(strSql);
            }
        }
        #endregion
        #region 获取通知信息
        /// <summary>
        /// 获取通知信息
        /// 创 建 人:LiuY
        /// 创建时间:2009-04-15
        /// </summary>
        /// <param name="RealUserId"></param>
        /// <returns></returns>
        public DataTable GetNotiyUser(string RealUserId)
        {
            string strSql = string.Format(@"select * from ST_NOTIFYUSER t,st_other a where t.sid=a.id and a.is_notice=1 and t.readflag = '0' and t.login_name='{0}'", RealUserId);
            DataTable dt;
            SysParams.OAConnection().RunSql(strSql, out dt);
            return dt;
        }
        #endregion

        #region 更新阅读标志位
        public void UpdateNotiyReadFlag(string RealUserId, string sid)
        {
            string strSql = string.Format(@"update ST_NOTIFYUSER t set t.readflag='1' where t.login_name='{0}' and t.sid = '{1}'", RealUserId, sid);
            SysParams.OAConnection().RunSql(strSql);
        }
        #endregion

        ////////////////////////新增-LSJ
        /// <summary>
        /// 记录关闭消息的人员信息
        /// </summary>
        /// <param name="strUserid"></param>
        /// <param name="strYkg"></param>
        /// <param name="strWjg"></param>
        /// <param name="strWkg"></param>
        /// <returns></returns>
        public static void UpdateNoticeFlag(string strUserid, string strYkg, string strWjg, string strWkg)
        {
            string strSql = string.Format(@"insert into USER_CLICK_NOTICE t(USERID,YKG,WJG,WKG) 
                values('{0}','{1}','{2}','{3}') ", strUserid, strYkg, strWjg, strWkg);

            SysParams.OAConnection().RunSql(strSql);
        }
    }
}
